using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using RaisingStudio.Razor.Models;

namespace RaisingStudio.Razor
{
    public class Migrations : DataMigrationImpl
    {
        public int Create()
        {
            // Creating table RazorContentPartRecord
            SchemaBuilder.CreateTable("RazorContentPartRecord",
                table => table
                .ContentPartRecord()
                .Column<int>("ContentItemRecord_id")
                .Column<string>("Template", column => column.Unlimited())
            );

            // Add the AlterPartDefinition lines to the migration in order to make the part attachable to any content type.
            ContentDefinitionManager.AlterPartDefinition(typeof(RazorContentPart).Name, cfg => cfg.Attachable());

            // Create a new widget content type with RazorContentPart.
            ContentDefinitionManager.AlterTypeDefinition("RazorWidget",
                cfg => cfg
                    .WithPart("WidgetPart")
                    .WithPart("RazorContentPart")
                    .WithPart("CommonPart")
                    .WithPart("IdentityPart")
                    .WithSetting("Stereotype", "Widget")
            );


            ContentDefinitionManager.AlterTypeDefinition("RazorPage",
                    cfg => cfg
                    .WithPart("CommonPart", p => p
                        .WithSetting("DateEditorSettings.ShowDateEditor", "true"))
                    .WithPart("PublishLaterPart")
                    .WithPart("TitlePart")
                    .WithPart("AutoroutePart", builder => builder
                        .WithSetting("AutorouteSettings.AllowCustomPattern", "true")
                        .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
                        .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Title', Pattern: '{Content.Slug}', Description: 'my-page'}]")
                        .WithSetting("AutorouteSettings.DefaultPatternIndex", "0"))
                                .WithPart("RazorContentPart")
                    .Creatable()
                    .Draftable()
             );

            return 1;
        }
    }
}